package com.pkdl.dao;

import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;

import com.pkdl.dto.ThuocDTO;

// Data access object, which get data of Thuoc table from database
public class ThuocDAO {
	public boolean delete(int mathuoc) {
		boolean result = true;
		Connection connect = null;
		try {
			connect = DataAccessHelper.getConnection();
			CallableStatement cs = connect
					.prepareCall("{call usp_Thuoc_Delete(?)}");
			cs.setInt(1, mathuoc);
			int n = cs.executeUpdate();
			if (n == 0) {
				result = false;
			}
			connect.close();
		} catch (SQLException ex) {
			Logger.getLogger(ThuocDAO.class.getName()).log(Level.SEVERE, null,
					ex);
		} finally {
			try {
				connect.close();
			} catch (SQLException ex) {
				Logger.getLogger(ThuocDAO.class.getName()).log(Level.SEVERE,
						null, ex);
			}
		}
		return result;
	}

	public boolean insert(ThuocDTO thuoc) {
		boolean result = true;
		Connection connect = null;
		try {
			connect = DataAccessHelper.getConnection();
			CallableStatement cs = connect
					.prepareCall("{call usp_Thuoc_Insert(?, ?, ?, ?, ?, ?)}");
			cs.setString(1, thuoc.getDonViTinh());
			cs.setString(2, thuoc.getTenThuoc());
			cs.setLong(3, thuoc.getGia());
			cs.setString(4, thuoc.getQuiCach());
			cs.setString(5, thuoc.getHamLuong());
			cs.setString(6, thuoc.getCachDung());
			int n = cs.executeUpdate();
			if (n == 0) {
				result = false;
			}
			connect.close();
		} catch (SQLException ex) {
			Logger.getLogger(ThuocDAO.class.getName()).log(Level.SEVERE, null,
					ex);
		} finally {
			try {
				connect.close();
			} catch (SQLException ex) {
				Logger.getLogger(ThuocDAO.class.getName()).log(Level.SEVERE,
						null, ex);
			}
		}
		return result;
	}

	public ArrayList<ThuocDTO> selectAll() {
		ArrayList<ThuocDTO> list = new ArrayList<ThuocDTO>();
		Connection connect = null;
		try {
			connect = DataAccessHelper.getConnection();
			CallableStatement cs = connect
					.prepareCall("{call usp_Thuoc_SelectAll()}");
			ResultSet rs = cs.executeQuery();
			while (rs.next()) {
				ThuocDTO thuoc = new ThuocDTO();
				thuoc.setMaThuoc(rs.getInt("MaThuoc"));
				thuoc.setDonViTinh(rs.getString("DonViTinh"));
				thuoc.setTenThuoc(rs.getString("TenThuoc"));
				thuoc.setGia(rs.getLong("Gia"));
				thuoc.setQuiCach(rs.getString("QuiCach"));
				thuoc.setHamLuong(rs.getString("HamLuong"));
				thuoc.setCachDung(rs.getString("CachDung"));
				list.add(thuoc);
			}
			connect.close();
		} catch (SQLException ex) {
			Logger.getLogger(ThuocDAO.class.getName()).log(Level.SEVERE, null,
					ex);
		} finally {
			try {
				connect.close();
			} catch (SQLException ex) {
				Logger.getLogger(ThuocDAO.class.getName()).log(Level.SEVERE,
						null, ex);
			}
		}
		return list;
	}

	public ThuocDTO selectById(int mathuoc) {
		ThuocDTO thuoc = null;
		Connection connect = null;
		try {
			connect = DataAccessHelper.getConnection();
			CallableStatement cs = connect
					.prepareCall("{call usp_Thuoc_SelectByID(?)}");
			cs.setInt(1, mathuoc);
			ResultSet rs = cs.executeQuery();
			while (rs.next()) {
				thuoc = new ThuocDTO();
				thuoc.setMaThuoc(rs.getInt("MaThuoc"));
				thuoc.setDonViTinh(rs.getString("DonViTinh"));
				thuoc.setTenThuoc(rs.getString("TenThuoc"));
				thuoc.setGia(rs.getLong("Gia"));
				thuoc.setQuiCach(rs.getString("QuiCach"));
				thuoc.setHamLuong(rs.getString("HamLuong"));
				thuoc.setCachDung(rs.getString("CachDung"));
			}
			connect.close();
		} catch (SQLException ex) {
			Logger.getLogger(ThuocDAO.class.getName()).log(Level.SEVERE, null,
					ex);
		} finally {
			try {
				connect.close();
			} catch (SQLException ex) {
				Logger.getLogger(ThuocDAO.class.getName()).log(Level.SEVERE,
						null, ex);
			}
		}
		return thuoc;
	}

	public boolean update(ThuocDTO thuoc) {
		boolean result = true;
		Connection connect = null;
		try {
			connect = DataAccessHelper.getConnection();
			CallableStatement cs = connect
					.prepareCall("{call usp_Thuoc_Update(?, ?, ?, ?, ?, ?, ?)}");
			cs.setInt(1, thuoc.getMaThuoc());
			cs.setString(2, thuoc.getDonViTinh());
			cs.setString(3, thuoc.getTenThuoc());
			cs.setLong(4, thuoc.getGia());
			cs.setString(5, thuoc.getQuiCach());
			cs.setString(6, thuoc.getHamLuong());
			cs.setString(7, thuoc.getCachDung());
			int n = cs.executeUpdate();
			if (n == 0) {
				result = false;
			}
			connect.close();
		} catch (SQLException ex) {
			Logger.getLogger(ThuocDAO.class.getName()).log(Level.SEVERE, null,
					ex);
		} finally {
			try {
				connect.close();
			} catch (SQLException ex) {
				Logger.getLogger(ThuocDAO.class.getName()).log(Level.SEVERE,
						null, ex);
			}
		}
		return result;
	}

}
